package com.yowasa.xiuxian.engine.callback.map;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yowasa.xiuxian.annoations.NOTIFY;
import com.yowasa.xiuxian.engine.callback.CmdCallBackHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.ClickMsgComponent;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExploreResource;
import com.yowasa.xiuxian.entity.po.XiuXianExploreUser;
import com.yowasa.xiuxian.entity.po.XiuxianMapInfoPO;
import com.yowasa.xiuxian.service.helper.ExploreHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.ComponentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@NOTIFY(tag = "探索地点")
public class ExploreCallBackHandler extends CmdCallBackHandler {

    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private ExploreHelper exploreHelper;

    @Autowired
    private UserHelper userHelper;


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void execute(ClickMsgComponent message) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setRoom_id(message.getRoom_id());
        sendMessage.setVilla_id(message.getVilla_id());
        String uid = message.getUid();
        sendMessage.setFrom_user_id(uid);
        JSONObject jsonObject = JSONUtil.parseObj(message.getExtra());
        //jsonObject.put("mapId",mapList.get(i).getId());
        Integer mapId = jsonObject.getInt("mapId");
        UserBO userBO = userHelper.checkOpUserAndState(sendMessage);
        XiuXianExploreUser exploreUser = exploreHelper.selectUser(userBO.getId());
        XiuxianMapInfoPO xiuxianMapInfoPO = exploreHelper.checkMap(mapId, exploreUser);
        List<XiuXianExploreResource> resources = exploreHelper.selectResourceByMapId(mapId);
        List<MysComponent> components = new ArrayList<>();
        for (int i = 0 ;i < resources.size();i++){
            if(resources.get(i).getType() != 1){
                MysComponent component = new MysComponent((i+1)+"",1,resources.get(i).getResourceName(),"");
                JSONObject object = new JSONObject();
                object.put("key","探索资源");
                object.put("mapId",mapId);
                object.put("resourceId",resources.get(i).getId());
                component.setExtra(object.toString());
                components.add(component);
            }
        }
        MysComponent[] mysComponents = components.toArray(new MysComponent[]{});
        List<List<MysComponent>> lists = ComponentUtil.genComponentList(2, mysComponents);
        messageHelper.replyMysPanel(sendMessage,"前往同一区域的不同地点需要3天时间\n"+xiuxianMapInfoPO.getName()+"资源",null,lists,null);

        List<MysComponent> componentsA = new ArrayList<>();
        for (int i = 0 ;i < resources.size();i++){
            if(resources.get(i).getType() == 1){
                MysComponent component = new MysComponent((i+1)+"",1,resources.get(i).getResourceName(),"");
                JSONObject object = new JSONObject();
                object.put("key","探索资源");
                object.put("mapId",mapId);
                object.put("resourceId",resources.get(i).getId());
                component.setExtra(object.toString());
                componentsA.add(component);
            }
        }
        MysComponent[] mysComponentsA = componentsA.toArray(new MysComponent[]{});
        List<List<MysComponent>> listsA = ComponentUtil.genComponentList(2, mysComponentsA);
        messageHelper.replyMysPanel(sendMessage,"前往同一区域的不同地点需要5天时间\n"+xiuxianMapInfoPO.getName() + "怪物",null,listsA,null);

        List<XiuXianExploreUser> exploreUserList = exploreHelper.selectLocalUserByMapId(mapId,userBO.getId());
        List<MysComponent> componentsB = new ArrayList<>();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("前往同一区域的不同地点需要5天时间\n").append(xiuxianMapInfoPO.getName()).append("存在玩家 :");
        for (int i = 0 ;i < exploreUserList.size();i++){
            MysComponent component = new MysComponent((i+1)+"",1,exploreUserList.get(i).getAccountId().toString(),"");
            JSONObject object = new JSONObject();
            object.put("key","抢劫");
            object.put("accountId",exploreUserList.get(i).getAccountId());
            component.setExtra(object.toString());
            componentsB.add(component);
            stringBuffer.append("\n").append(exploreUserList.get(i).getAccountId()).append("  ").append(exploreUserList.get(i).getName());
        }
        MysComponent[] mysComponentsB = componentsB.toArray(new MysComponent[]{});
        List<List<MysComponent>> listsB = ComponentUtil.genComponentList(2, mysComponentsB);

        messageHelper.replyMysPanel(sendMessage,stringBuffer.toString(),null,listsB,null);

    }



}
